#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "guicontainerform.h"
#include "homepage.h"
#include "calculatorinterface.h"
#include "quit.h"
#include "clock.h"

#include <QLabel>
#include <QTimer>
#include <QPushButton>
#include <QDebug>
#include <QDateTime>
#include <QStackedWidget>
#include <QCloseEvent>
#include <QDesktopWidget>

QString MainWindow::staffID;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),m_container(new GuiContainerForm)
{
    ui->setupUi(this);
    this->setCentralWidget(m_container);

    m_quit = new Quit(this);
    m_calculatorInterface = new CalculatorInterface(this);
    m_clock = new Clock(this);
    m_close = false;
    m_date = new QLabel(this);

    m_quit->hide();
    m_clock->hide();
    m_calculatorInterface->hide();

    //设置自定义的鼠标指针图案
//    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),-1,-1);
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    setWindowIcon(QIcon(":/Title/Image/title/title.png"));
//    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool );
//    setWindowTitle(tr("YiFei Supermarket Cash Register System"));
    setWindowTitle(tr("                                         (☆_☆) 熠飞超市收银系统 (☆_☆)"));

//    this->setAutoFillBackground(true);
////    QPixmap pixmap(":/homepage/Image/homePage/background/ShowerPhotos.jpg");
//    QPixmap pixmap(":/homepage/Image/homePage/background/LinYunEr.jpg");
//    QPalette palette;
//    palette.setBrush(QPalette::Window, QBrush(pixmap.scaled(size(),
//                    Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)));
//    setPalette(palette);

    m_container->setMinimumWidth(1050);
    m_container->setMinimumHeight(600);

    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(soltUpdateDateTime()));
    m_timer->start(1000);

    connect(m_quit, SIGNAL(exit()), this, SLOT(slotClose()));

    m_date->installEventFilter(this);
    QWidget::installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::CreatStatusBar(const QString &staff_ID)
{
    QFont font(tr("文泉驿等宽正黑"), 12, 2, false);

    QLabel *staff_ID_Label = new QLabel;
    staff_ID_Label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    staff_ID_Label->setMinimumSize(staff_ID_Label->sizeHint());
    staff_ID_Label->setAlignment(Qt::AlignHCenter);
    staff_ID_Label->setText(tr("登录用户 : ") + staff_ID);
    staff_ID_Label->setStyleSheet("color:blue; border: 2px solid #EE30A7");
    staff_ID_Label->setFont(font);
    staffID = staff_ID;

//    m_date = new QLabel(this);
    m_date->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    m_date->setMinimumSize(m_date->sizeHint());
    m_date->setAlignment(Qt::AlignHCenter);
    m_date->setStyleSheet("color:blue; border: 2px solid #EE30A7");
    m_date->setFont(font);
    soltUpdateDateTime();

    QLabel *QQLabel = new QLabel;
    QQLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    QQLabel->setMinimumSize(QQLabel->sizeHint());
    QQLabel->setAlignment(Qt::AlignHCenter);
    QQLabel->setText(tr("QQ支持 : 308847694"));
    QQLabel->setStyleSheet("color:blue; border: 2px solid #EE30A7");
    QQLabel->setFont(font);

    QLabel *EmailLabel = new QLabel;
    EmailLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    EmailLabel->setMinimumSize(EmailLabel->sizeHint());
    EmailLabel->setAlignment(Qt::AlignHCenter);
    EmailLabel->setText("E-mail : panyifei1992@qq.com");
    EmailLabel->setStyleSheet("color:blue; border: 2px solid #EE30A7");
    EmailLabel->setFont(font);

    QPushButton *calculator = new QPushButton;
    calculator->setFixedSize(45, 23);
//image: url(:/MainWindow/Image/mainwindow/calculator.png);
    calculator->setStyleSheet("QPushButton{image: url(:/MainWindow/Image/mainwindow/calculator.png); "
                            "border: 2px solid #8f8f91;border-radius: 6px;background-color: "
                            "qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);} "
                            "QPushButton:flat {border: none;} "
                            "QPushButton:default {border-color: navy;}");
    calculator->connect(calculator, SIGNAL(clicked()), this, SLOT(soltCalculatorClicked()));

    QPushButton *HomePage = new QPushButton;
    HomePage->setFixedSize(147, 25);
    HomePage->setStyleSheet("QPushButton{image: url(:/MainWindow/Image/mainwindow/HomePage.png); "
                            "border: 2px solid #8f8f91;border-radius: 6px;background-color: "
                            "qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);} "
                            "QPushButton:hover{image: url(:/MainWindow/Image/mainwindow/HomePageHover.png);} "
                            "QPushButton:pressed {image: url(:/MainWindow/Image/mainwindow/HomePagePress.png);} "
                            "QPushButton:flat {border: none;} "
                            "QPushButton:default {border-color: navy;}");
    HomePage->connect(HomePage, SIGNAL(clicked()), this, SLOT(soltHomePageClicked()));

    QLabel *NULLLabel = new QLabel;

    ui->statusbar->setStyleSheet(QString("QStatusBar::item{border:0px}"));
    ui->statusbar->addWidget(m_date);
    ui->statusbar->addWidget(staff_ID_Label);
    ui->statusbar->addWidget(QQLabel);
    ui->statusbar->addWidget(EmailLabel);
    ui->statusbar->addWidget(calculator);
    ui->statusbar->addWidget(NULLLabel ,1);
    ui->statusbar->addWidget(HomePage);
}

//void MainWindow::closeEvent(QCloseEvent *event)
//{
//    if (m_close)
//        event->accept();
//    else
//        event->ignore();

//    m_quit->setWindowFlags(
//                Qt::WindowStaysOnTopHint | Qt::Tool | Qt::Dialog);

//    m_quit->setWindowFlags(
//                Qt::Dialog );
//    m_quit->move((QApplication::desktop()->width() - m_quit->width())/2,
//                     (QApplication::desktop()->height() - m_quit->height())/2);
//    //在对 QWidget 操作时，不能操作其他东西
//    m_quit->setWindowModality(Qt::ApplicationModal);
//    m_quit->setAttribute(Qt::WA_ShowModal, true);
//    m_quit->show();

//    if (m_quit == NULL)
//        m_quit = new Quit(this);
//    m_quit->setWindowFlags(Qt::Dialog);
//    m_quit->move((QApplication::desktop()->width() - m_quit->width())/2,
//                 (QApplication::desktop()->height() - m_quit->height())/2);
//    //在对 QWidget 操作时，不能操作其他东西
//    m_quit->setWindowModality(Qt::ApplicationModal);
//    m_quit->setAttribute(Qt::WA_ShowModal, true);
//    m_quit->show();

//}

bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
    if (event->type() == QEvent::Close)
    {
        if (!m_close)
            event->ignore();

        m_quit->setWindowFlags(Qt::Dialog);
        m_quit->move((QApplication::desktop()->width() - m_quit->width())/2,
                     (QApplication::desktop()->height() - m_quit->height())/2);
        //在对 QWidget 操作时，不能操作其他东西
        m_quit->setWindowModality(Qt::ApplicationModal);
        m_quit->setAttribute(Qt::WA_ShowModal, true);
        m_quit->activateWindow();
        m_quit->show();
        return true;
    }
    if (target == m_date)
    {
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == m_date)
                {
//                    m_clock->setWindowFlags(
//                                Qt::FramelessWindowHint | Qt::Dialog );
                    m_clock->setWindowFlags(
                                Qt::Dialog );
//                    m_clock->setWindowFlags(
//                                Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool );
                    QPoint GlobalPoint(m_date->mapToGlobal(QPoint(0, 0)));
                    int x = GlobalPoint.x();//该控件在窗体中左上角横坐标
                    int y = GlobalPoint.y();//该控件在窗体中左上角纵坐标
                    m_clock->setGeometry(x, y - 505, 500, 500);
                    //在对 QWidget 操作时，不能操作其他东西
                    m_clock->setWindowModality(Qt::ApplicationModal);
                    m_clock->setAttribute(Qt::WA_ShowModal, true);
                    m_clock->show();
                }
            }
        }
    }

    return QWidget::eventFilter(target, event);
}

void MainWindow::displayStockRemind()
{
    m_container->on_pushButtonStockPrompt_clicked();
}

void MainWindow::soltUpdateDateTime()
{
    QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
    QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); //设置显示格式
    m_date->setText(str);//在标签上显示时间
}

void MainWindow::soltHomePageClicked()
{
    m_container->m_StackedWidget->setCurrentWidget(m_container->m_homepage);
}

void MainWindow::soltCalculatorClicked()
{
    m_calculatorInterface->setWindowFlags(
                Qt::WindowStaysOnTopHint | Qt::Tool );
    m_calculatorInterface->setWindowFlags(
                Qt::Dialog );
    m_calculatorInterface->move((QApplication::desktop()->width() - m_calculatorInterface->width())/2,
                     (QApplication::desktop()->height() - m_calculatorInterface->height())/2);
    //在对 QWidget 操作时，不能操作其他东西
    m_calculatorInterface->setWindowModality(Qt::ApplicationModal);
    m_calculatorInterface->setAttribute(Qt::WA_ShowModal, true);
    m_calculatorInterface->show();
}

void MainWindow::slotClose()
{
    m_close = true;
    this->close();
}
